//入口文件

import './css/index.css' //初始化css
import './lib/mui/css/mui.min.css' //导入mui样式 哈哈
import './lib/mui/css/icons-extra.css' //导入扩展图标

//导入 vue
import Vue from 'vue'
//按需导入 miutui

import { Header,Swipe,SwipeItem,Button,Lazyload,Switch } from 'mint-ui'
Vue.use(Lazyload)
Vue.component(Header.name,Header)
Vue.component(Swipe.name,Swipe)
Vue.component(SwipeItem.name,SwipeItem)
Vue.component(Button.name,Button)
Vue.component(Switch.name,Switch)


//vuex 使用
import Vuex from 'vuex'
Vue.use(Vuex)
//进入系统就读取本地存储的car
var car = JSON.parse(localStorage.getItem('car'))||[];

const store = new Vuex.Store({
    state:{
        car:car,//开辟公用数据时，直接使用本地存储的数据
    },
    mutations:{
        getcardata(state,goodsinfo){
            var existflag = false  
            // console.log(goodsinfo)
            
            //如果发现 car中有相同id的，则更新count就可以
            state.car.some(item=>{
                if(item.id == goodsinfo.id){
                    item.count += parseInt(goodsinfo.count)
                    existflag = true
                    return true
                }
            })
            
            // //如果car中没有
            if(!existflag){
                state.car.push(goodsinfo);
            }
            localStorage.setItem('car',JSON.stringify(state.car))
        },
        //购物车中的数量变更 mutation方法
        updatedata(state,goodsinfo){
            state.car.some(item=>{
                if(item.id == goodsinfo.id){
                    switch(goodsinfo.type){
                        case 'count':
                            item.count = parseInt(goodsinfo.count)
                            break
                        case 'selected':
                            item.selected = goodsinfo.selected
                            break
                    }
                    return true
                }
            })
            localStorage.setItem('car',JSON.stringify(state.car))
        },
        //删除购物车中的指定id商品
        removedata(state,id){
            state.car.some((item,i)=>{
                if(item.id == id){
                    state.car.splice(i,1)
                    return true
                }
            })
            localStorage.setItem('car',JSON.stringify(state.car))
        }

    },
    getters:{
        amount(state){
            var o = 0
            
            state.car.forEach(item=>{
                o += item.count
            })
            return o
        },
        shopcarids(state){
            var o = []
            state.car.forEach(item=>{
                o.push(item.id)
            })
            return o
        },
        goodscountlist(state){
            var o = []
            state.car.forEach(item=>{
                o[item.id] = item.count
            })
            return o
        },
        goodsselectlist(state){
            var o = []
            state.car.forEach(item=>{
                o[item.id] = item.selected
            })
            return o
        },
        selectedamountprices(state){
            var o = {
                amount:0,
                prices:0
            }
            state.car.forEach(item=>{
                if(item.selected){
                    o.amount += item.count
                    o.prices += item.count * item.sell_price
                }
            })
            return o
        },
        

    }
})


//导入resource
import VueResource from 'vue-resource'
//使用resource
Vue.use(VueResource)
Vue.http.options.root = 'http://vue.studyit.io';
Vue.http.options.emulateJSON = true;
//导入路由 
import VueRouter from 'vue-router'
Vue.use(VueRouter)
//导入preview
// import 'swipe/dist/css/swiper.css'
import VuePreview from "vue-preview"
Vue.use(VuePreview)
import router from './router.js'

import app from './App.vue'
import moment from 'moment'
Vue.filter("dateFormat",(dateStr,pattern="YYYY-MM-DD HH:mm:ss")=>{
    return moment(dateStr*1000).format(pattern)
})

var vm = new Vue({
    el:'#app',
    render:c=>c(app),
    router,
    store
    // directives:{
    //     focus:{
    //         inserted:function(el){el.focus()}
    //     }
    // }
})